package com.me.batm.models;

import java.util.ArrayList;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.me.batm.GameConstants;
import com.me.batm.Utils;

public class Player extends Person {
	public static final float WIDTH=50f,HEIGHT=60f, SPEED = 1f, LIFE = 1;
	public Vector2 center;
	public Vector2 p1,p2,p3,p4;
	public float timeOfLife;
	public float bloodLost;
	public float speedFactor;
	public float lifeFactor;
	public boolean reverseControl;
	public boolean blurryView;
	public ArrayList<TimedEntity> timedAlterations;
	public ArrayList<Float> alterationsTime;
	
	public static int LEFT = 0;
	public static int RIGHT = 1;
	public static int DOWN = 2;
	public static int UP = 3;
	public Vector2[] directionMultiplier = {
			new Vector2(-1, 0),
			new Vector2(1, 0),
			new Vector2(0, -1),
			new Vector2(0, 1)
	};
	
	public Player(float x, float y, Vector2 direction) {
		super(x, y, WIDTH,HEIGHT, direction);
		
		// reset enum
		LEFT = 0; RIGHT = 1; DOWN = 2; UP = 3;
		
		bloodLost = 0f;
		speedFactor = SPEED;
		lifeFactor = LIFE;
		reverseControl = false;
		blurryView = false;
		timedAlterations = new ArrayList<TimedEntity>();
		alterationsTime = new ArrayList<Float>(); 
		refreshPosition();
		
		teletransportTime = 100f;
	}
	
	private void refreshPosition(){
		center = new Vector2(shape.x + WIDTH / 2f, shape.y + HEIGHT/2f);

		p1 = Utils.rotateAround(new Vector2(shape.x,shape.y),center,getAngle());
		p2 = Utils.rotateAround(new Vector2(shape.x+Player.WIDTH,shape.y),center,getAngle());
		p3 = Utils.rotateAround(new Vector2(shape.x+Player.WIDTH,shape.y + Player.HEIGHT ),center,getAngle());
		p4 = Utils.rotateAround(new Vector2(shape.x,shape.y + Player.HEIGHT),center,getAngle());
	}

	public float getAngle(){
		return direction.angle();		
	}
	
	public void addTimedAlteration(TimedEntity entity, float remainingTime) {
		timedAlterations.add(entity);
		alterationsTime.add(remainingTime);
	}
	
	public void updateTimedAlterations(float delta) {
		int i;
		float time;
		for (i = 0; i < alterationsTime.size(); i += 1) {
			time = alterationsTime.get(i);
			time -= delta;
			if (time <= 0) {
				timedAlterations.get(i).removeEffectOverPlayer(this);
				timedAlterations.remove(i);
				alterationsTime.remove(i);
			} else {
				alterationsTime.set(i, time);
			}
		}
	}
	
	public void move(float delta, Vector2 moveVector,boolean movePerson) {		
		if(moveVector.x != 0f || moveVector.y != 0f){//There is some user input
			keyFrameTime += delta;
			if(movePerson){
				shape.x += moveVector.x * GameConstants.PLAYER_SPEED_CONSTANT * speedFactor;
				shape.y += moveVector.y * GameConstants.PLAYER_SPEED_CONSTANT * speedFactor;
			}
			
			direction = new Vector2(0f,0f);
			
			if(moveVector.x < 0f)direction.x = -1f;
			if(moveVector.x > 0f)direction.x = +1f;
			if(moveVector.y < 0f)direction.y = -1f;
			if(moveVector.y > 0f)direction.y = +1f;
				
			/*
			if(moveVector.x == 0f)
				direction.x *= 0.8f;
			else
				direction.x += GameConstants.SHIP_ROTATE_CONSTANT * moveVector.x;
			
			if(moveVector.y == 0f)
				direction.y *= 0.8f;
			else
				direction.y += GameConstants.SHIP_ROTATE_CONSTANT * moveVector.y;
			*/
			
			/*
			direction.x = Math.min(1,Math.max(-1f,direction.x));//[-1,1]
			direction.y = Math.min(1,Math.max(-1f,direction.y));//[-1,1]
			*/
			refreshPosition();
		}else{//No user input			
		}

		
		substractLife(delta * (100f/timeOfLife) * lifeFactor );
	}
	
	public void substractLife(float damage) {
		life -= damage;
		bloodLost += damage;
		if(life < 0f) {
			life = 0f;
			kill();
		}
	}
	
	public boolean getBloodLost() {
		if(isAlive && bloodLost >= 0.1f) {
			bloodLost -= 0.1f;
			return true;
		}
		return false;
	}

	public void setTimeOfLife(float timeOfLife) {
		this.timeOfLife = timeOfLife;
	}
}
